package cn.stylefeng.guns.modular.shuheng.service;

import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.guns.modular.shuheng.entity.Files;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.stylefeng.guns.modular.shuheng.mapper.FilesMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author zhengpp
 * @since 2021-03-19
 */
@Service
public class FilesService extends ServiceImpl<FilesMapper,Files> {
    public Page<Map<String,Object>> listFiles(String condition, Long parentId, Integer year, Integer level, Long city, Long county, BigDecimal score,Integer status,String[] quarterArr) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.selectFiles(page,condition,parentId,year,level,city,county,score,status,quarterArr);
    }

    public Files findByDataId(Long dataId,ShiroUser user) {
        QueryWrapper<Files> wrapper = new QueryWrapper<>();
        wrapper.eq("data_id",dataId);
        wrapper.eq("region_level",user.getUserLevel());
        wrapper.eq("province",user.getProvince());
        if(ToolUtil.isEmpty(user.getCity())){
            wrapper.isNull("city");
        }else{
            wrapper.eq("city",user.getCity());
        }
        if(ToolUtil.isEmpty(user.getCounty())){
            wrapper.isNull("county");
        }else{
            wrapper.eq("county",user.getCounty());
        }
        return this.baseMapper.selectOne(wrapper);
    }

    public Files getFilesByDataIdAndUser(Long dataId, ShiroUser user) {
        return this.baseMapper.findByDataIdAndUser(dataId,user.getUserLevel(),user.getProvince(),user.getCity(),user.getCounty());
    }

    public List<Files> listByUser(ShiroUser user,Integer year,String[] monthArr) {
        Integer userLevel = user.getUserLevel();
        Long county = user.getCounty();
        if(userLevel==2){
            //如果是市级管理员登录。导出的时候连带区县的一块导出
            userLevel = null;
            county = null;
        }
        return this.baseMapper.listByUser(userLevel,user.getProvince(),user.getCity(),county,year,monthArr);
    }

    /**
     * 上传任务时删除已完成的任务
     * 如果county为空，那就删除市级及市以下区县的
     * @param year
     * @param city
     * @param county
     */
    public void deleteByYear(Integer year,Long city,Long county){
        QueryWrapper<Files> wrapper = new QueryWrapper<>();
        wrapper.eq("year",year);
        wrapper.eq("city",city);
        if(county!=null){
            wrapper.eq("county",county);
        }
        this.baseMapper.delete(wrapper);
    }

    public List<Map<String, Object>> listExcelFiles(String condition, Long parentId, Integer year, Integer level, Long city, Long county, BigDecimal score,Integer status,String[] quarterArr) {
        return this.baseMapper.listExcelFiles(condition,parentId,year,level,city,county,score,status,quarterArr);
    }

    public List<Files> listByDept(Long deptId) {
        return this.baseMapper.listByDept(deptId);
    }

    /**
     * 列出可选年份
     * @return
     */
    public List<Map<String,Object>> listYear() {
        return this.baseMapper.listYear();
    }
}
